package com.hsd.znsh.service.impl;

import com.hsd.znsh.entity.*;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.properties.SecurityProperties;
import com.hsd.znsh.repository.*;
import com.hsd.znsh.service.SensorTypeService;
import com.hsd.znsh.utils.BeanUtil;
import com.hsd.znsh.utils.FileUtil;
import com.hsd.znsh.utils.HqlUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-25 15:58:26
 */
@Service
public class SensorTypeServiceImpl extends BaseServiceImpl implements SensorTypeService {

    @Autowired
    private SensorTypeRepository sensorTypeRepository;

    @Autowired
    private PropertyRepository propertyRepository;

    @Autowired
    private SensorTypePropertyRepository sensorTypePropertyRepository;

    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private SensorRepository sensorRepository;

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public long getSensorTypeListTotalPages(SensorType sensorType) {
        return this.count(HqlUtil.getCountHql(sensorType),sensorType);
    }


    @Override
    public List<SensorType> getSensorTypeList(Pageable pageable, SensorType sensorType) {
        return this.find(HqlUtil.getQueryHql(sensorType),sensorType,pageable);
    }

    @Override
    public SensorType getSensorTypeInfo(Integer id) {
        return sensorTypeRepository.findOne(id);
    }

    @Override
    public void addSensorType(SensorType sensorType,MultipartFile sensorTypeFile,
                              List<Property> propertyList,List<MultipartFile> propertyFile) {
        String filePath=securityProperties.getBrowser().getFilePath();
        String logo= FileUtil.copyToFile(sensorTypeFile,filePath+"/sensor/sensorType");
        sensorType.setLogo(logo);
        sensorTypeRepository.save(sensorType);
        saveProperty(sensorType,propertyList,propertyFile);
    }

    @Override
    public void updateSensorType(SensorType sensorType,MultipartFile sensorTypeFile,
                                 List<Property> propertyList,List<MultipartFile> propertyFile) {
        String filePath=securityProperties.getBrowser().getFilePath();
        List<Integer> propertyIds=sensorTypePropertyRepository.findPropertyIdListBySensorTypeId(sensorType.getId());
        Collections.sort(propertyIds);

        for(int i=propertyList.size();i<propertyIds.size();i++){
            Property property=propertyRepository.findOne(propertyIds.get(i));
            sensorTypePropertyRepository.deleteByPropertyId(property.getId());
            propertyRepository.delete(property);
            FileUtil.delFile(property.getIcon());
        }
        if(sensorTypeFile!=null){
            FileUtil.delFile(sensorType.getLogo());
            String logo= FileUtil.copyToFile(sensorTypeFile,filePath+"/sensor/sensorType");
            sensorType.setLogo(logo);
        }
        SensorType target=sensorTypeRepository.findOne(sensorType.getId());
        BeanUtil.copyPropertiesIgnoreNull(sensorType,target);
        sensorTypeRepository.save(target);
        saveProperty(target,propertyList,propertyFile);

    }

    private void saveProperty(SensorType sensorType,List<Property> propertyList,List<MultipartFile> propertyFile){
        Integer bh=sensorTypePropertyRepository.findMaxBhBySensorTypeId(sensorType.getId());
        if(bh==null) bh=0;
        String parentPath=securityProperties.getBrowser().getFilePath()+"/sensor/property/"+sensorType.getId();
        for(int i=0;i<propertyList.size();i++){
            Property property=propertyList.get(i);
            if(property.getId()==null){
                String icon=FileUtil.copyToFile(propertyFile.get(i),parentPath);
                property.setIcon(icon);
                propertyRepository.save(property);

                SensorTypeProperty sensorTypeProperty=new SensorTypeProperty();
                sensorTypeProperty.setSensorTypeId(sensorType.getId());
                sensorTypeProperty.setPropertyId(property.getId());
                sensorTypeProperty.setBh(++bh);
                sensorTypePropertyRepository.save(sensorTypeProperty);
            }else {
                MultipartFile multipartFile=propertyFile.get(i);
                Property target=propertyRepository.findOne(property.getId());
                if(multipartFile!=null){
                    FileUtil.delFile(target.getIcon());
                    String icon=FileUtil.copyToFile(propertyFile.get(i),parentPath);
                    property.setIcon(icon);
                }
                BeanUtil.copyPropertiesIgnoreEmpty(property,target);
                propertyRepository.save(target);
            }
        }
    }

    @Override
    public void deleteSensorType(Integer id) {
        SensorType sensorType=sensorTypeRepository.findOne(id);
        String filePath=securityProperties.getBrowser().getFilePath();
        List<Integer> propertyIds=sensorTypePropertyRepository.findPropertyIdListBySensorTypeId(id);
        sensorTypePropertyRepository.deleteBySensorTypeId(id);
        if(propertyIds!=null){
            FileUtil.delFolder(filePath+"/sensor/property/"+id);
            propertyRepository.deleteByIdIn(propertyIds);
        }
        FileUtil.delFile(sensorType.getLogo());
        sensorTypeRepository.delete(id);
    }

    @Override
    public List<SensorType> getAllSensorTypeList() {
        return sensorTypeRepository.findAll();
    }

    @Override
    public List<Property> getPropertyBySensorTypeId(Integer sensorTypeId) {
        List<Integer> propertyIds=sensorTypePropertyRepository.findPropertyIdListBySensorTypeId(sensorTypeId);
        return propertyRepository.findByIdInOrderById(propertyIds);
    }

    @Override
    public List<SensorType> getSensorTypeListByHostSn(String hostSn) {
        Host host=hostRepository.findByHostSn(hostSn);
        List<Integer> sensorTypeId=sensorRepository.findSensorTypeListByHostId(host.getId());
        return sensorTypeRepository.findByIdIn(sensorTypeId);
    }
}
